package com.tanhua.server.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.TanHuaService;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("tanhua")
public class TanHuaController {

    @Autowired
    private TanHuaService tanHuaService;

    /**
     * 查看佳人信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/{id}/personalInfo")
    public ResponseEntity<TodayBest> findBestUserInfo(@PathVariable("id") Long userId) {
        try {
            TodayBest todayBest = tanHuaService.findBestUserInfo(userId);
            if (ObjectUtil.isNotEmpty(todayBest)) {
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> findStrangerQuestion(@RequestParam("userId") Long userId) {
        try {
            String question = tanHuaService.findStrangerQuestion(userId);
            if (null != question) {
                return ResponseEntity.ok(question);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 回复陌生人问题
     *
     * @param param
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> answerQuestion(@RequestBody Map<String, Object> param) {
        Long userId = Convert.toLong(param.get("userId").toString());
        String reply = param.get("reply").toString();
        try {
            Boolean result = tanHuaService.answerQuestion(userId, reply);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
